package common;

import java.io.*;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2023/12/21 20:33
 */
public class FileUtil {

    //负责把filePath对应的文件内容读取出来放到返回值中
    public static String readFile(String filePath) {
        StringBuilder result = new StringBuilder();
        //try执行完之后自动关闭文件流
        try (FileReader fileReader = new FileReader(filePath)){
            while (true){
                int ch = fileReader.read();
                if (ch==-1){
                    break;
                }
                result.append((char)ch);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();

    }

    //负责把content 写入到filePath中
    public static void writeFile(String filePath,String content) {
        try(FileWriter fileWriter = new FileWriter(filePath)){
            fileWriter.write(content);
            System.out.println("写入java文件的路径"+filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        FileUtil.writeFile("d:/test1.txt","hello --");
        String content = readFile("d:/test1.txt");
        System.out.println(content);
    }




}
